import * as inventorySheetService from '../../services/inventorySheet';
import * as inventoryService from '../../services/inventory';
import {queryRoomList} from '../../services/tRoom';
import moment from 'moment'

export default {
  namespace: 'inventorySheet',
  state: {
    list: [],  
    roomList:[],  
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    currentViewItem:{},
    detailList:[],
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0, 

  },
  reducers: {  
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },        
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },   
    searchAssetSuc(state, { payload }) {
      return { ...state, ...payload };
    },  
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },  
     
  },
  effects: {    
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },      

    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(inventoryService.onViewItem,payload);     
      yield put({
          type: 'showModal',
          payload: {
            currentViewItem: body.data,
            viewModalType: 'view',
            title: '查看盘点详情',
          },
      });
       yield put({ type: 'searchAsset',payload:{sheetId:payload,status:""}});
    },  

    *searchAsset({ payload },{ call, put }) {
      const { body } = yield call(inventoryService.searchAsset,payload);
      yield put({ type: 'searchAssetSuc', payload: { detailList:body.data, assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});
    },

    *queryList({ payload },{ call, put }){
      const { body } = yield call(inventorySheetService.queryList,payload);
      yield put({ 
        type: 'queryListSuc', 
         payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords }
      });
    },  

    *reload({ payload },{ call, put }){
       yield put({ type: 'queryList',payload});
    },      
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/inventorySheet') {
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom' });          
         
        }
      });
    },
  },
};
